<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithTitle; //sheet名称
use Maatwebsite\Excel\Concerns\WithMultipleSheets;//sheet
use Maatwebsite\Excel\Concerns\WithStyles;//造型
use Maatwebsite\Excel\Concerns\WithColumnWidths; //列宽
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
class UsersExport implements FromCollection, WithMultipleSheets, WithTitle, WithColumnWidths, WithStyles, WithColumnFormatting
    {
        use Exportable;
        private $data;//订单数据
        private $status; //sheet名称（订单状态）
        private $column; //总行数
        private $goodsNum = []; //一个订单的商品数量
        /**
         * sheet名称
         * @return string
         */
        public function title(): string
        {
            return $this->status;
        }
        /**
         * 设置列宽
         * @return array
         */
        public function columnWidths(): array
        {
            return [
                'A' => 16, 'B' => 13, 'C' => 13, 'D' => 20, 'E' => 50, 'F' => 8, 'G' => 10, 'H' => 13,
                'I' => 8, 'J' => 12, 'K' => 12, 'L' => 12, 'M' => 20, 'N' => 20, 'O' => 15, 'P' => 20,
                'Q' => 20, 'R' => 20, 'S' => 30, 'T' => 30, 'U' => 20, 'V' => 20, 'W' => 20, 'X' => 20,
            ];
        }
        /**
         * 格式化列
         * @return array
         */
        public function columnFormats(): array
        {
            $format = NumberFormat::FORMAT_NUMBER_00;//金额保留两位小数
            return ['G' => $format, 'H' => $format, 'I' => $format, 'J' => $format, 'K' => $format];
        }

        /**
         * 样式设置
         * @param Worksheet $sheet
         * @throws \PhpOffice\PhpSpreadsheet\Exception
         */
        public function styles(Worksheet $sheet)
        {
            $sheet->getDefaultRowDimension()->setRowHeight(22);//设置行高
            $sheet->getStyle('A1:Z' . $this->column)->getAlignment()->setVertical('center');//垂直居中
            $sheet->getStyle('F1:K' . $this->column)->applyFromArray(['alignment' => ['horizontal' => 'center']]);//设置水平居中
            $sheet->getStyle('A1:Z1')->applyFromArray(['font' => ['bold' => true, 'color' => ['rgb' => '0072ff']]]);//字体设置
            $cell = ['A', 'B', 'C', 'D', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'];//需要合并的单元格
            //$sheet->mergeCells('A18:A22'); //合并单元格
            foreach ($cell as $item) {
                $start = 2;
                foreach ($this->goodsNum as $key => $value) {
                    $end = $start + $value - 1;
                    $sheet->mergeCells($item . $start . ':' . $item . $end); //合并单元格
                    $start = $end + 1;
                }
            }
        }
        public function __construct($row,$data)
        {
            $this->row = $row;
            $this->data = $data;
        }
    
        public function collection()
        {
            $row = $this->row;
            $data = $this->data;
    
            //设置表头
            foreach ($row[0] as $key => $value) {
                $key_arr[] = $key;
            }
    
            //输入数据
            foreach ($data as $key => &$value) {
                $js = [];
                for ($i=0; $i < count($key_arr); $i++) {
                    $js = array_merge($js,[ $key_arr[$i] => $value[ $key_arr[$i] ] ]);
                }
                array_push($row, $js);
                unset($val);
            }
            return collect($row);
        }

    }